/**
 * Writed by yura and mak
 */

package wz.legend.gui;

import java.awt.event.ActionEvent;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import wz.legend.db.DBManager;
import wz.legend.handlers.auction.BuyManager;
import wz.legend.handlers.auction.BuyProduct;
import wz.legend.util.ConfigFileStorage;
import wz.legend.util.FileStorage;


/**
 * 
 * @author yura
 */
public class BuyingProductPanel extends javax.swing.JFrame {

    private JButton closeButton;
    private JPanel jPanel2;
    private JPanel jPanel3;
    private JScrollPane jScrollPane1;
    private JTable jTable1;
    private JButton saveButton;
    private JButton addRowButton;
    private DefaultTableModel tableModel;

    private FileStorage fileStorage = new ConfigFileStorage(
            BuyManager.FILE_NAME, "UTF-8");

    public BuyingProductPanel() {
        initComponents();
        loadProducts();
    }

    private void loadProducts() {
        List<BuyProduct> itemList = (List<BuyProduct>) fileStorage.loadStream();
        if (itemList == null) {
            itemList = new ArrayList<BuyProduct>();
        }
        tableModel.setRowCount(0);

        // Collections.sort(itemList);
        for (BuyProduct product : itemList) {
            Object[] objects = new Object[6];
            objects[0] = product.getName();
            objects[1] = product.getCost();
            objects[2] = product.getLevel();
            objects[3] = product.getAmount();
            objects[4] = product.getQualityLow();
            objects[5] = product.getQualityHi();
            tableModel.addRow(objects);
        }
    }

    private void saveProducts() {
        List<BuyProduct> itemList = new ArrayList<BuyProduct>();
        for (int i =0; i < tableModel.getRowCount(); i++){
            BuyProduct product = new BuyProduct();
            product.setName((String)tableModel.getValueAt(i, 0));
            product.setCost((Integer)tableModel.getValueAt(i, 1));
            product.setLevel((Integer)tableModel.getValueAt(i, 2));
            product.setAmount((Integer)tableModel.getValueAt(i, 3));
            product.setQualityLow((Integer)tableModel.getValueAt(i, 4));
            product.setQualityHi((Integer)tableModel.getValueAt(i, 5));
            itemList.add(product);
        }
        int j = 0;
    }

    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        closeButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        addRowButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(500, 300));

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel2.setLayout(new java.awt.BorderLayout());

        tableModel = new javax.swing.table.DefaultTableModel(new Object[][] {},
                new String[] { "Product", "Cost", "Level", "amount", "min Q",
                        "max Q" }) {
            Class[] types = new Class[] { java.lang.String.class,
                    java.lang.Integer.class, java.lang.Integer.class,
                    java.lang.Integer.class, java.lang.Integer.class,
                    java.lang.Integer.class };

            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }
        };

        jTable1.setModel(tableModel);
        jTable1.setColumnSelectionAllowed(true);
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getSelectionModel().setSelectionMode(
                javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jTable1.getColumnModel().getColumn(1).setMinWidth(50);
        jTable1.getColumnModel().getColumn(1).setPreferredWidth(50);
        jTable1.getColumnModel().getColumn(1).setMaxWidth(50);
        jTable1.getColumnModel().getColumn(2).setMinWidth(50);
        jTable1.getColumnModel().getColumn(2).setPreferredWidth(50);
        jTable1.getColumnModel().getColumn(2).setMaxWidth(50);
        jTable1.getColumnModel().getColumn(3).setMinWidth(50);
        jTable1.getColumnModel().getColumn(3).setPreferredWidth(50);
        jTable1.getColumnModel().getColumn(3).setMaxWidth(50);
        jTable1.getColumnModel().getColumn(4).setMinWidth(50);
        jTable1.getColumnModel().getColumn(4).setPreferredWidth(50);
        jTable1.getColumnModel().getColumn(4).setMaxWidth(50);
        jTable1.getColumnModel().getColumn(5).setMinWidth(50);
        jTable1.getColumnModel().getColumn(5).setPreferredWidth(50);
        jTable1.getColumnModel().getColumn(5).setMaxWidth(50);

        jPanel2.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel3.setMinimumSize(new java.awt.Dimension(20, 200));
        jPanel3.setPreferredSize(new java.awt.Dimension(20, 45));

        closeButton.setText("Close");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        saveButton.setText("Save");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });
        
        addRowButton.setText("Add row");
        addRowButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addRowButtonActionPerformed(evt);
            }
       });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(
                jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout
                .setHorizontalGroup(jPanel3Layout
                        .createParallelGroup(
                                javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(
                                javax.swing.GroupLayout.Alignment.TRAILING,
                                jPanel3Layout
                                        .createSequentialGroup()
                                        .addContainerGap(467, Short.MAX_VALUE)
                                        .addComponent(addRowButton)
                                        .addPreferredGap(
                                                javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(saveButton)
                                        .addPreferredGap(
                                                javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(closeButton)
                                        .addContainerGap()));
        jPanel3Layout
                .setVerticalGroup(jPanel3Layout
                        .createParallelGroup(
                                javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(
                                javax.swing.GroupLayout.Alignment.TRAILING,
                                jPanel3Layout
                                        .createSequentialGroup()
                                        .addContainerGap(
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                Short.MAX_VALUE)
                                        .addGroup(
                                                jPanel3Layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(
                                                                closeButton)
                                                        .addComponent(
                                                                saveButton).addComponent(addRowButton))
                                        .addContainerGap()));

        getContentPane().add(jPanel3, java.awt.BorderLayout.SOUTH);

        pack();
    }

    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {
        dispose();
    }

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {
        saveProducts();
    }

    private void addRowButtonActionPerformed(ActionEvent evt) {
        Object[] objects = new Object[6];
        objects[0] = new String();
        objects[1] = new Integer(0);
        objects[2] = new Integer(0);
        objects[3] = new Integer(0);
        objects[4] = new Integer(0);
        objects[5] = new Integer(0);
        tableModel.addRow(objects);
    }

    /**
     * @param args
     *            the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new BuyingProductPanel().setVisible(true);
            }
        });
    }

}
